package com.hfzy.ihk.web.freeswitchManage.controller;

import com.hfzy.ihk.facade.freeswitchManage.entity.NumResource;
import com.hfzy.ihk.facade.freeswitchManage.entity.SipRegistrations;
import com.hfzy.ihk.facade.freeswitchManage.entity.Userinfo;
import com.hfzy.ihk.facade.freeswitchManage.enums.SwxTrunkStatusEnum;
import com.hfzy.ihk.web.freeswitchManage.constant.RedisKeys;
import com.hfzy.ihk.web.freeswitchManage.enums.ClientType;
import com.hfzy.ihk.web.freeswitchManage.mapper.CopShowNumMapper;
import com.hfzy.ihk.web.freeswitchManage.mapper.SipRegistrationsMapper;
import com.hfzy.ihk.web.freeswitchManage.mapper.UserinfoMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by YANFA on 2019/3/12.
 */
@RestController
@RequestMapping("initiOpt")
@Slf4j
public class FreeswitchOpt {

    @Autowired
    RedisTemplate<String,Object> redisTemplate;

    @Autowired
    UserinfoMapper userinfoMapper;

    @Autowired
    SipRegistrationsMapper sipRegistrationsMapper;

    @Autowired
    CopShowNumMapper copShowNumMapper;



    /**
     * freeswitch 用户初始化注册接口，有则修改密码，没有则新增
     * @param userName
     * @param clientType
     * @return
     */
    @RequestMapping("initiUser")
    public Map<String,Object> initiUser(String userName,String password,String clientType){
        Map<String,Object> map = new HashMap<>();

        try {
            if (clientType == null || userName == null) {
                map.put("code", 0);
                map.put("msg", "必填参数不能为空");
                return map;
            }

            //---判断该客户端类型是否存在系统枚举中 ---假装是个白名单
            if(!ClientType.existClientType(clientType.toUpperCase())){
                map.put("code", -2);
                map.put("msg", "客户端类型不存在");
                return map;
            }

            //-------验证该账号是否存在
            Userinfo userinfo = userinfoMapper.queryUserinfoByUserName(userName);

            if (userinfo == null) {//不存在则插入
                Userinfo myuserinfo = new Userinfo();
                myuserinfo.setUsername(userName);
                myuserinfo.setClientType(clientType);
                if(password==null){
                    myuserinfo.setPassword("ihkyanfa");
                }else{
                    myuserinfo.setPassword(password);
                }

                userinfoMapper.insert(myuserinfo);
            } else {
                if (userinfo.getClientType()!=null&&!clientType.equals(userinfo.getClientType())) {
                    map.put("code", -3);
                    map.put("msg", "该用户已经存在");
                    return map;

                }
            }

            //-------验证该账号是否已登录

            SipRegistrations sipRegistrations = sipRegistrationsMapper.querySipRegistrationsByUserName(userName);
            if(sipRegistrations!=null){
                map.put("code", -4);
                map.put("msg", "该用户已经登录，请勿重复登录");
                return map;
            }

            map.put("code", 1);
            map.put("msg", "成功");
        }catch (Exception e){
            log.error("验证用户失败userName:{}",userName,e);
            map.put("code", -1);
            map.put("msg", "验证用户失败");
        }
        return map;
    }
//
//    @RequestMapping("queryIdleChannel")
//    public Map<String,String> queryIdleChannel(){
//
//        Map<String,String> map = new HashMap<>();
//
//        Map<String,Object>  channelStateMap = redisTemplate.opsForHash().entries(RedisKeys.SwxTrunkStatus);
//        if(channelStateMap==null||channelStateMap.size()==0){
//            map.put("code","0");
//            map.put("msg","无可用通道");
//            return map;
//        }
//
//        for(Map.Entry<String,Object> entry: channelStateMap.entrySet()){
//            if(entry!=null&& SwxTrunkStatusEnum.IDLE.getValue().equals(entry.getValue())){
//                map.put("code","1");
//                map.put("msg","有可用通道");
//                break;
//
//            }
//        }
//        return map;
//
//
//    }



    @RequestMapping("initiCopShowNum")
    public Map<String,String> initiCopShowNum(String source){

        Map<String,String> map = new HashMap<>();
        String rs = "1";
        String msg = "成功";
        //主显号相关
        String CUCC_SHOW_NUM_INDEX = "copShowNumIndex:";

        String CUCC_SHOW_NUM_LIST = "copShowNumList:";

        String aliyunShowNumIndex = "aliyunShowNumIndex:";
        String aliyunShowNumList = "aliyunShowNumList:";

        //查询数据控中
        List<NumResource> allList = copShowNumMapper.getAllShowNumBySource(source);
        if(allList==null||allList.size()==0){
            map.put("rs","-1");
            map.put("msg","无数据");
            return  map;
        }
        //初始化前先删除在插入
        try {
            if("cop".equals(source)) {
                redisTemplate.delete(CUCC_SHOW_NUM_INDEX + "1"); //删除所有key
                redisTemplate.delete(CUCC_SHOW_NUM_INDEX + "2");
                redisTemplate.delete(CUCC_SHOW_NUM_INDEX + "3");
                redisTemplate.delete(CUCC_SHOW_NUM_INDEX + "140");
                redisTemplate.delete(CUCC_SHOW_NUM_LIST + "1");
                redisTemplate.delete(CUCC_SHOW_NUM_LIST + "2");
                redisTemplate.delete(CUCC_SHOW_NUM_LIST + "3");
                redisTemplate.delete(CUCC_SHOW_NUM_LIST + "140");

                for (NumResource numResource : allList) {
                    redisTemplate.opsForList().rightPush(CUCC_SHOW_NUM_LIST + numResource.getCity(), numResource.getShowNum());
                }
            }else if("aliyun".equals(source)){
                redisTemplate.delete(aliyunShowNumIndex + "1"); //删除所有key
                redisTemplate.delete(aliyunShowNumIndex + "2");
                redisTemplate.delete(aliyunShowNumIndex + "3");
                redisTemplate.delete(aliyunShowNumIndex + "140");
                redisTemplate.delete(aliyunShowNumList + "1");
                redisTemplate.delete(aliyunShowNumList + "2");
                redisTemplate.delete(aliyunShowNumList + "3");
                redisTemplate.delete(aliyunShowNumList + "140");

                for (NumResource numResource : allList) {
                    redisTemplate.opsForList().rightPush(aliyunShowNumList + numResource.getCity(), numResource.getShowNum());
                }
            }

        }catch (Exception e){
            log.error("初始化cop主显号异常",e);
            rs = "-1";
            msg = "异常";
        }
        map.put("rs",rs);
        map.put("msg",msg);
        return  map;
    }




}
